import 'package:flutter/material.dart';
import 'package:gd_assistant/navigator/tabNavigator.dart';
import 'loginPage.dart';
import 'util/dataUtil.dart';
import 'package:gd_assistant/page/public/colors.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  realRunApp();
}

void realRunApp() async {
  await SpUtil.getInstance();
  runApp(LoginApp());
} 


class LoginApp extends StatefulWidget {
  @override
  _LoginAppState createState() => _LoginAppState();
}

class _LoginAppState extends State<LoginApp> {
  bool isLogin = true;
 
  @override
  void initState() {
    super.initState();
    isLogoIn();
  }

  void isLogoIn() async {
    String token = dataUtil.getValue(dataUtil.tokenKey);
    setState(() {
       isLogin = (token != null);
    });
  }

  ThemeData getThemeData(bool isDarkMode) {
    return ThemeData(
        errorColor: isDarkMode ? Colours.dark_red : Colours.red,
        brightness: isDarkMode ? Brightness.dark : Brightness.light,
        primaryColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
        accentColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
        // Tab指示器颜色
        indicatorColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
        // 页面背景色
        scaffoldBackgroundColor: isDarkMode ? Colours.dark_bg_color : Colors.white,
        // 主要用于Material背景色
        canvasColor: isDarkMode ? Colours.dark_material_bg : Colors.white,
        // 文字选择色（输入框复制粘贴菜单）
        textSelectionColor: Colours.app_main.withAlpha(70),
        textSelectionHandleColor: Colours.app_main,
        appBarTheme: AppBarTheme(
          elevation: 0.0,
          color: isDarkMode ? Colours.dark_bg_color : Colors.blue,
          brightness: isDarkMode ? Brightness.dark : Brightness.light,
        ),
        dividerTheme: DividerThemeData(
          color: isDarkMode ? Colours.dark_line : Colours.line,
          space: 0.6,
          thickness: 0.6
        )
      );
  }

   @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        theme: getThemeData(false),
        darkTheme: getThemeData(true),
        home: isLogin ? tabNavigator() : LoginPage()
        );
  }
}

